<template>
  <div class="table-main">
    <el-dialog
      @close="handMyClose"
      :append-to-body="true"
      :close-on-click-modal="false"
      title="角色信息"
      :visible.sync="dialogVisible1"
      width="60%"
    >
      <div style="margin-top:50px;" v-loading="loading2">
        <el-form ref="form" :model="form" label-width="120px" :rules="rules">
          <el-row :gutter="20" justify="center" type="flex">
            <el-col :span="16">
              <el-form-item label="角色名:" prop="roleName">
                <el-input :disabled="isShow == 3" v-model="form.roleName"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20" justify="center" type="flex" v-if="isShow == 3">
            <el-col :span="16">
              <el-form-item label="角色编码:" prop="roleValue">
                <el-input disabled v-model="form.roleValue"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20" justify="center" type="flex">
            <el-col :span="16">
              <el-form-item label="角色描述:" prop="roleValue">
                <el-input :disabled="isShow == 3" v-model="form.roleValue"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20" justify="center" type="flex">
            <el-col :span="16">
              <el-form-item label="菜单权限:" prop="sort">
                <el-tree
                  @check-change="checkChange"
                  :data="options"
                  show-checkbox
                  node-key="id"
                  :props="defaultProps"
                  :default-checked-keys="optionsKey"
                  :default-expanded-keys="optionsKey"
                  ref="tree"
                ></el-tree>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible1 = false">取 消</el-button>
        <el-button
          v-if="isShow != 3"
          type="primary"
          @click="handFromSub('form')"
          :loading="bntLoading"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { imgSizi } from '@/utils/myUtils'
export default {
  name: 'SidebarLogo',
  components: {},
  watch: {
    isState(newObj) {
      this.isShow = newObj
    }
  },
  props: {
    isState: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      bntLoading: false,
      headers: {
        token: localStorage.getItem('mytoken') || ''
      },
      rules: {
        roleName: [
          { required: true, message: '请输入商品类别名字', trigger: 'blur' }
        ],
        typeImageUrl: [{ required: true, message: '请上传商品背景图' }]
      },
      defaultParams: {
        label: 'title',
        value: 'id',
        children: 'children',
        checkStrictly: true
      },
      form: {
        // prentId: "-1"
      },
      shopStatusOptions: [],
      isShow: 0,
      imageUrl: '',
      dialogImageUrl: '',
      dialogVisible1: false,
      addressKeyword: '',
      activeName: 'first',
      dialogVisible: false,
      options: [],
      loading2: true,
      showID: '',
      flg: '',
      optionsKey: []
    }
  },
  mounted() {},
  methods: {
    handData(data) {
      console.log(data)
    },
    handGoodsTypeTreeinfo(id, flg, row) {
      this.flg = flg
      this.options = []
      this.loading2 = false
      this.$api.getGoodsTypeTreeinfo().then(res => {
        this.options = res.data.data
        this.options.forEach(item => {
          item.id = parseInt(item.id)
        })
        console.log(res.data, '树形菜单', this.options)
      })
    },
    handGetShop(id, row) {
      this.loading2 = true
      console.log(id, '?')
      this.$api.getByIdGoodsType({ id }).then(res => {
        res.data.data.permissionid = res.data.data.permissionid.split(',')
        for (let i = 0; i < res.data.data.permissionid.length; i++) {
          res.data.data.permissionid[i] = parseInt(
            res.data.data.permissionid[i]
          )
        }
        this.optionsKey = res.data.data.permissionid
        this.form = res.data.data
        this.loading2 = false
        if (this.isShow == 3) {
          this.options.forEach(item => {
            item.disabled = true
            item.children.forEach(ele => {
              ele.disabled = true
            })
          })
        }
        console.log(res, '查看角色信息')
      })
    },
    handSuccess(res, file) {
      // this.imageUrl = URL.createObjectURL(file.raw)
      var url = JSON.parse(res.message)
      this.$set(this.form, 'typeImageUrl', url.url)
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2
      const isJPG = file.type === 'image/jpeg'
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 2MB!')
      }
      const isSize = imgSizi(file, 92, 92)
      return isSize
    },
    handSuccess2(res, file) {
      // this.imageUrl = URL.createObjectURL(file.raw)
      var url = JSON.parse(res.message)
      this.$set(this.form, 'bgImg', url.url)
    },
    beforeAvatarUpload2(file) {
      const isLt2M = file.size / 1024 / 1024 < 2
      const isJPG = file.type === 'image/jpeg'
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 2MB!')
      }
      const isSize = imgSizi(file, 750, 262)
      return isSize
    },
    handMyClose() {
      for (let key in this.form) {
        this.form[key] = ''
      }
      this.optionsKey = []
    },
    handFromSub(formName) {
      this.form.permissionid = this.ids
      // 1编辑
      if (this.isState == 1) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            this.bntLoading = true
            let shop = this.form
            delete this.form.createTime
            console.log(this.form, 'this.form')
            this.$api
              .getEditGoodsType(this.form)
              .then(res => {
                this.dialogVisible1 = false
                this.$parent.init()
                this.bntLoading = true
                this.$message({
                  message: res.data.message,
                  type: 'success'
                })
              })
              .finally(() => {
                this.bntLoading = false
              })
          } else {
            console.log('error submit!!')
            this.$message({
              message: '请完成表单录入'
            })
            return false
          }
        })
      } else if (this.isState == 0) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            this.bntLoading = true
            this.$api
              .getAddGoodsType(this.form)
              .then(res => {
                this.dialogVisible1 = false
                this.bntLoading = false
                this.$parent.init()
                this.$refs.form.resetFields()
                console.log(res, '添加', this.form)
                this.$message({
                  message: res.data.message,
                  type: 'success'
                })
              })
              .finally(() => {
                this.bntLoading = false
              })
          } else {
            console.log('error submit!!')
            this.$message({
              message: '请完成表单录入'
            })
            return false
          }
        })
      }
    },

    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    handleClose() {
      this.dialogVisible = false
    },
    handMapShow() {
      this.dialogVisible = true
    },
    handleClick() {},
    checkChange(data1, data2, data3) {
      let res = this.$refs.tree
        .getCheckedKeys()
        .concat(this.$refs.tree.getHalfCheckedKeys())
      this.ids = res.join(',')
      console.log('ceshi', this.ids)
    }
  }
}
</script>

<style lang="scss" scoped>
.map_btn {
  width: 82px;
  height: 22px;
  background: rgba(56, 173, 147, 1);
  border-radius: 4px;
  color: #fff;
  font-size: 14px;
  line-height: 22px;
  text-align: center;
  margin-top: 8px;
}
/deep/ .el-form-item__label {
  font-size: 14px;
  color: #000;
  font-weight: bold;
}
.form_box {
  width: 80%;
  margin: 0 auto;
}
.avatar-uploader /deep/ .el-upload {
  border: 1px dashed #38ad93;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #38ad93;
  width: 46px;
  height: 46px;
  line-height: 46px;
  text-align: center;
}
.avatar {
  width: 46px;
  height: 46px;
  display: block;
}
.avatar2 {
  width: 375px;
  //  750, 262
  height: 131px;
  display: block;
}
.avatar-uploader-icon2 {
  font-size: 28px;
  color: #38ad93;
  width: 375px;
  height: 131px;
  line-height: 131px;
  text-align: center;
}
</style>
